<template>
	<view class="container"><yxylInputs :inputs="inputs" @confirm="confirm"></yxylInputs></view>
</template>

<script>
import yxylInputs from '@/components/yxyl-inputs/yxyl-inputs.vue';
export default {
	components: { yxylInputs },
	data() {
		return {
			inputs: []
		};
	},
	onLoad() {
		this.bindVolunteer().then(() => {
			// #ifdef H5
			this.$wechat.hideMenu();
			// #endif
		});
	},
	methods: {
		bindVolunteer() {
			var that = this;
			return this.$http
				.request({
					url: 'api/Volunteer/GetVolunteer'
				})
				.then(res => {
					that.inputs.push({ title: '地区', keys: ['provinceId', 'cityId'], type: 'address', level: 2, default: res ? [res.provinceId, res.cityId] : null });
					that.inputs.push({
						title: '姓名',
						key: 'userName',
						placeholder: '请输入名字',
						required: true,
						type: 'text',
						errMsg: '请输入姓名',
						default: res ? res.userName : ''
					});
					that.inputs.push({
						title: '电话号码',
						key: 'telephone',
						placeholder: '请输入电话号码',
						required: true,
						type: 'number',
						errMsg: '请输入电话号码',
						default: res ? res.telephone : ''
					});
					that.inputs.push({
						title: '身份证',
						key: 'idCard',
						placeholder: '请输入身份证',
						required: true,
						type: 'number',
						errMsg: '请输入身份证',
						default: res ? res.idCard : ''
					});
					that.inputs.push({ title: '个人简介', key: 'introduction', placeholder: '请输入个人简介', type: 'text', default: res ? res.introduction : '' });
					that.inputs.push({
						title: '性别',
						key: 'sexuality',
						type: 'radio',
						range: [{ text: '男', value: '1', checked: res ? res.sexuality == 1 : true }, { text: '女', value: '2', checked: res ? res.sexuality == 2 : false }],
						default: res ? res.sexuality : 1
					});
					that.inputs.push({ title: '志愿服务内容', key: 'serviceContent', placeholder: '请输入志愿服务内容', type: 'text', default: res ? res.serviceContent : '' });
					that.inputs.push({ title: '可服务时间', key: 'serviceDateTime', placeholder: '请输入可服务时间', type: 'text', default: res ? res.serviceDateTime : '' });
					that.inputs.push({
						title: '资料是否开放',
						key: 'isOpen',
						type: 'radio',
						range: [{ text: '是', value: true, checked: res ? res.isOpen : true }, { text: '否', value: false, checked: res ? !res.isOpen : false }],
						default: res ? res.isOpen : true
					});
					
					console.log(that.inputs)
				});
		},
		confirm(e) {
			this.$http
				.request({
					url: 'api/Volunteer/CreateOrUpdateVolunteer',
					method: 'PUT',
					data: e
				})
				.then(res => {
					uni.showToast({
						icon: 'none',
						title: '保存成功',
						success() {
							setTimeout(function() {
								if (getCurrentPages().length > 1) {
									uni.navigateBack();
								} else {
									uni.switchTab({
										url: '../../user_center/user_center'
									});
								}
							}, 1500);
						}
					});
				});
		}
	}
};
</script>

<style></style>
